package com.ourcraft.informationcollectionsystem.service.province.investigationManage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ourcraft.informationcollectionsystem.entity.InvestigationPeriod;
import com.ourcraft.informationcollectionsystem.entity.InvestigationPeriodPattern;
import com.ourcraft.informationcollectionsystem.mapper.InvestigationPeriodMapper;
import com.ourcraft.informationcollectionsystem.mapper.InvestigationPeriodPatternMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.net.StandardSocketOptions;
import java.sql.Date;
import java.time.LocalDate;

/**
 * 用于调查期模式创建新的调查期
 *
 * @author Z0136
 * @since 2023/11/4
 */
@Service
@EnableScheduling
public class InvestigationCreatorImpl implements InvestigationCreator {
    @Autowired
    InvestigationPeriodMapper investigationPeriodMapper;
    @Autowired
    InvestigationPeriodPatternMapper investigationPeriodPatternMapper;
    @Autowired
    InvestigationManageServiceImpl investigationManageService;

    /**
     * 所有调查期模式定期自动创建调查期
     */
    @Override
    @Scheduled(cron = "0 0 0 * * *") // 每天0点执行一次
    public void createInvestigationsByPatterns() {
        QueryWrapper<InvestigationPeriodPattern> queryWrapper = new QueryWrapper<>();
        // 仅判断激活状态的调查期
        queryWrapper.eq("pattern_status", true);
        investigationPeriodPatternMapper.selectList(queryWrapper)
                .forEach(this::createInvestigationByPattern);
    }

    /**
     * 根据调查期模式创建新的调查期
     *
     * @param pattern 需要创建调查期的模式
     */
    @Override
    public void createInvestigationByPattern(InvestigationPeriodPattern pattern) {
//        System.out.println(pattern.getPatternId());
        // 创建一个当前日期的Date对象
        Date currentDate = new Date(new java.util.Date().getTime());
        // Date.before判断需要终止日期加一天
        Date endDate = Date.valueOf(pattern.getPatternValidEndDate().toLocalDate().plusDays(1));

        // 判断当前日期是否在有效开始日期和有效结束日期之间
        if (currentDate.after(pattern.getPatternValidStartDate()) && currentDate.before(endDate)) {
            // 计算当前日期和有效开始日期之间的天数差
            long diff = (currentDate.getTime() - pattern.getPatternValidStartDate().getTime()) / (24 * 60 * 60 * 1000);
            // 判断当前日期和有效开始日期之间的天数差是否为有效间隔的倍数
            if (diff % pattern.getPatternInterval() == 0) {
                // 创建一个查询条件，按照investigation_start_date降序排列，取一条记录
                QueryWrapper<InvestigationPeriod> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("investigation_start_date").last("LIMIT 1");
                // 查询investigation_start_date降序排列的记录
                InvestigationPeriod lastInvestigation = investigationPeriodMapper.selectOne(queryWrapper);
                // 获取查询结果的investigation_start_date
                Date lastDate = lastInvestigation != null ? lastInvestigation.getInvestigationStartDate() : null;

                // 生成调查期title
                String title = (lastDate == null ? "" : lastDate + "~") + currentDate + " " + pattern.getPatternName();
                // 创建新的investigation
                investigationManageService.createInvestigation(
                        new InvestigationPeriod(pattern.getPatternId(),
                                title, currentDate, pattern.getPatternInvLength(), title));
            }
        }
    }
}
